package cn.net.xyan

import cn.net.xyan.kotlin.logger
import cn.net.xyan.pay.core.service.ButtonType
import cn.net.xyan.pay.core.service.DefaultMenuManager
import cn.net.xyan.pay.core.service.MenuManagerService
import cn.net.xyan.pay.core.service.button
import cn.net.xyan.pay.core.support.BeanPropertiesDefinition
import cn.net.xyan.pay.core.support.BeanPropertiesPostProcessor
import cn.net.xyan.pay.core.support.VendorUserInfoHandlerMethodArgumentResolver
import cn.net.xyan.pay.wechat.WeChatConfiguration
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
import org.springframework.scheduling.annotation.Async
import org.springframework.scheduling.annotation.EnableAsync
import javax.annotation.PostConstruct
import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
import org.springframework.web.servlet.config.annotation.EnableWebMvc
import org.dom4j.dom.DOMNodeHelper.setPrefix
import org.springframework.web.servlet.view.InternalResourceViewResolver
import org.springframework.web.servlet.ViewResolver





open class MenuJob {

    @Autowired
    lateinit open var menuManager: MenuManagerService

    @PostConstruct
    open fun setup() {
        doJob()
    }

    @Async
    open fun doJob() {
        menuManager.createMenu(listOf(
                button {
                    name = "医院之家"
                    child {
                        name = "医院通知Page2"
                        type = ButtonType.URL
                        content = "page1"
                    }
                    child {
                        name = "常见问题"
                        type = ButtonType.CLICK
                        content = "常见问题"
                    }
                    child {
                        name = "就诊指南"
                        type = ButtonType.CLICK
                        content = "就诊指南"
                    }
                    child {
                        name = "坐诊安排"
                        type = ButtonType.CLICK
                        content = "坐诊安排"
                    }

                },
                button {
                    name = "个性服务"
                    child {
                        name = "wifi上网"
                        type = ButtonType.CLICK
                        content = "wifi上网"
                    }
                    child {
                        name = "预约挂号"
                        type = ButtonType.CLICK
                        content = "预约挂号"
                    }
                    child {
                        name = "最新活动"
                        type = ButtonType.CLICK
                        content = "最新活动"
                    }
                    child {
                        name = "意见建议"
                        type = ButtonType.CLICK
                        content = "意见建议"
                    }
                },
                button {
                    name = "个人中心"
                    child {
                        name = "就诊卡绑定"
                        type = ButtonType.CLICK
                        content = "就诊卡绑定"
                    }
                }
        ))
    }
}

@SpringBootApplication
@EntityScan("cn.net.xyan.pay.*")
@EnableJpaRepositories(basePackages = arrayOf("cn.net.xyan.pay.core.dao"))
@ComponentScan
@EnableAsync
@BeanPropertiesDefinition
class PayPlatformParentApplication {



    @Configuration
    //@EnableWebMvc
    open class WebMvcContext : WebMvcConfigurerAdapter() {

        @Autowired
        open lateinit var vendorUserInfoHandlerMethodArgumentResolver:VendorUserInfoHandlerMethodArgumentResolver

        @Bean
        open fun vendorUserInfoHandlerMethodArgumentResolver():VendorUserInfoHandlerMethodArgumentResolver{
            return VendorUserInfoHandlerMethodArgumentResolver()
        }

        @Bean
        open fun getViewResolver(): ViewResolver {
            val resolver = InternalResourceViewResolver()
            resolver.setPrefix("/WEB-INF/pages")
            resolver.setSuffix(".jsp")
            return resolver
        }

        override fun addArgumentResolvers(argumentResolvers: MutableList<HandlerMethodArgumentResolver>?) {
            argumentResolvers!!.add(vendorUserInfoHandlerMethodArgumentResolver)
        }
    }

//    @Bean
//    fun vendorUserInfoHandlerMethodArgumentResolver():HandlerMethodArgumentResolver{
//        return VendorUserInfoHandlerMethodArgumentResolver()
//    }

//    @Bean
//    fun beanPropertiesPostProcessor():BeanPropertiesPostProcessor{
//        return BeanPropertiesPostProcessor("classpath*:/bootstrap.properties")
//    }

    @Bean
    fun menuManager(): MenuManagerService {
        val menuManager = DefaultMenuManager()
        return menuManager
    }

    @Bean
    fun menuJob(): MenuJob {
        return MenuJob()
    }
//    @Configuration
//    class SecurityConfiguration : WebSecurityConfigurerAdapter() {
//
//
//        @Throws(Exception::class)
//        public override fun configure(auth: AuthenticationManagerBuilder?) {
//            auth!!.inMemoryAuthentication()
//                    .withUser("user").password("user").roles("USER")
//                    .and().withUser("admin").password("admin").roles("ADMIN");
//            //auth!!.userDetailsService<UserDetailsService>(securityService)
//        }
//
//        @Throws(Exception::class)
//        override fun configure(http: HttpSecurity) {
//            http.csrf().disable() // Use Vaadin's built-in CSRF protection instead
//            http.authorizeRequests().antMatchers("/ui/login/**").anonymous()
//                    .antMatchers("/login/**").anonymous()
//                    .antMatchers("/weChat**").anonymous()
//                    .antMatchers("/weChat/**").anonymous()
//                    .antMatchers("/ui/UIDL/**").permitAll()
//                    .antMatchers("/ui/HEARTBEAT/**").permitAll()
//                    .antMatchers("/vaadinServlet/UIDL/**").permitAll()
//                    .antMatchers("/vaadinServlet/HEARTBEAT/**").permitAll()
//                    .antMatchers("/public/**").permitAll()
//                    .antMatchers("/ui/public/**").permitAll()
//                    .anyRequest().authenticated()
//
//            //http.httpBasic().disable()
//            //http.formLogin().disable()
//            // Remember to add the VaadinSessionClosingLogoutHandler
////            http.logout().addLogoutHandler(VaadinSessionClosingLogoutHandler()).logoutUrl("/logout")
////                    .logoutSuccessUrl("/ui/login?logout").permitAll()
////            http.exceptionHandling().authenticationEntryPoint(LoginUrlAuthenticationEntryPoint("/ui/login"))
////            // Instruct Spring Security to use the same RememberMeServices as Vaadin4Spring. Also remember the key.
////            http.rememberMe().rememberMeServices(rememberMeServices()).key(RememberMeKey)
////            // Instruct Spring Security to use the same authentication strategy as Vaadin4Spring
////            http.sessionManagement().sessionAuthenticationStrategy(sessionAuthenticationStrategy())
//
//            http.headers().disable()
//        }
//
//        @Throws(Exception::class)
//        override fun configure(web: WebSecurity?) {
//            web!!.ignoring().antMatchers("/VAADIN/**")
//        }
//
//
//        @Bean
//        @Throws(Exception::class)
//        override fun authenticationManagerBean(): AuthenticationManager {
//            return super.authenticationManagerBean()
//        }
//
////        @Bean
////        fun rememberMeServices(): RememberMeServices {
////            return TokenBasedRememberMeServices(RememberMeKey, userDetailsService())
////        }
////
////        @Bean
////        fun sessionAuthenticationStrategy(): SessionAuthenticationStrategy {
////            return SessionFixationProtectionStrategy()
////        }
////
////        @Bean(name = arrayOf(VaadinSharedSecurityConfiguration.VAADIN_AUTHENTICATION_SUCCESS_HANDLER_BEAN))
////        internal fun vaadinAuthenticationSuccessHandler(httpService: HttpService,
////                                                        vaadinRedirectStrategy: VaadinRedirectStrategy): VaadinAuthenticationSuccessHandler {
////            return VaadinUrlAuthenticationSuccessHandler(httpService, vaadinRedirectStrategy, "/")
////        }
//    }

}

fun main(args: Array<String>) {
    SpringApplication.run(PayPlatformParentApplication::class.java, *args)
}
